// Copyright (c) 2007, Brian Duff
// See LICENSE for license details.

package org.dubh.orablogs.ag;

import java.io.Serializable;

import java.sql.Timestamp;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@NamedQueries({
  @NamedQuery(name = "Bloggers.findAll", query = "select o from Blog o"),
  @NamedQuery(name = "Bloggers.findById", query = "select o from Blog o where o.id = :id" )
})
@Table(name = "bloggers")
public class Blog
  implements Serializable
{
  @Column(name="allow_update", nullable = false)
  private String allow_update;
  @Column(name="article_count", nullable = false)
  private Integer article_count;
  @Column(name="blogname", nullable = false)
  private String blogname;
  @Column(name="blogurl", nullable = false)
  private String blogurl;
  @Column(name="feedurl", nullable = false)
  private String feedurl;
  @Column(name="forenames", nullable = false)
  private String forenames;
  @Id
  @Column(name="id", nullable = false)
  private Integer id;
  @Column(name="is_orcl")
  private String is_orcl;
  @Column(name="last_polled")
  private Timestamp last_polled;
  @Column(name="last_update")
  private Timestamp last_update;
  @Column(name="pollfreq")
  private Integer pollfreq;
  @Column(name="surname", nullable = false)
  private String surname;
  @Column(name="was_orcl", nullable = false)
  private String was_orcl;
  
  @OneToMany( mappedBy = "blogger" )
  private List<BlogEntry> entries;

  public Blog()
  {
  }

  public boolean isUpdateAllowed()
  {
    return stringToBoolean( allow_update );
  }

  public void setUpdateAllowed(boolean allow_update)
  {
    this.allow_update = booleanToString( allow_update );
  }

  public Integer getEntryCount()
  {
    return article_count;
  }

  public void setEntryCount(Integer article_count)
  {
    this.article_count = article_count;
  }

  public String getName()
  {
    return blogname;
  }

  public void setName(String blogname)
  {
    this.blogname = blogname;
  }

  public String getUrl()
  {
    return blogurl;
  }

  public void setUrl(String blogurl)
  {
    this.blogurl = blogurl;
  }

  public String getFeedUrl()
  {
    return feedurl;
  }

  public void setFeedUrl(String feedurl)
  {
    this.feedurl = feedurl;
  }

  public String getForenames()
  {
    return forenames;
  }

  public void setForenames(String forenames)
  {
    this.forenames = forenames;
  }

  public Integer getId()
  {
    return id;
  }

  public void setId(Integer id)
  {
    this.id = id;
  }

  public boolean isOracleEmployee()
  {
    return stringToBoolean( is_orcl );
  }

  public void setIsOracleEmployee( boolean isOracle )
  {
    this.is_orcl = booleanToString( isOracle );
  }

  public Timestamp getLastPolled()
  {
    return last_polled;
  }

  public void setLastPolled(Timestamp last_polled)
  {
    this.last_polled = last_polled;
  }

  public Timestamp getLastUpdate()
  {
    return last_update;
  }

  public void setLastUpdate(Timestamp last_update)
  {
    this.last_update = last_update;
  }

  public Integer getPollFrequency()
  {
    if ( pollfreq == null ) return 300000;
    return pollfreq;
  }

  public void setPollFrequency(Integer pollfreq)
  {
    this.pollfreq = pollfreq;
  }

  public String getSurname()
  {
    return surname;
  }

  public void setSurname(String surname)
  {
    this.surname = surname;
  }

  public String wasOracleEmployee()
  {
    return was_orcl;
  }

  public void setWasOracleEmployee(String was_orcl)
  {
    this.was_orcl = was_orcl;
  }
  
  

  public List<BlogEntry> entries()
  {
    return this.entries;
  }
  
  private static boolean stringToBoolean( String s )
  {
    return "Y".equals( s );
  }
  
  private static String booleanToString( boolean b )
  {
    return b ? "Y" : "N";
  }
}
